<?php
/**
 * Created By Project
 * Author: 陈东
 * Copyright：陈东
 * Source Code：陈东
 * Explain：I shall not authorize for any other commercial purposes, law breakers must be prosecuted.
 */
namespace Home\Controller;
use Think\Controller;

class ApplyController extends Controller {

    public function index(){

    	$this->display();
    }

	//中奖查询
	public function queryaward(){
		$key = I('post.key');
		if(!empty($key)){
			$applys = D(C('TABLE_APPLYS'));
			$res = $applys->where(array('mobile'=>$key))->select();
			$this->assign('key',$key);
			$this->assign('result',$res);
		}else{
			$this->error('参数不能为空','',1);
		}
		$this->display('main');

	}

	//设置中奖领取状态
	public function changestatus(){
		$id = I('post.id');
		$applys = D(C('TABLE_APPLYS'));
		$data['status'] = 1;
		$res = $applys->where(array('id'=>$id))->save($data);
		if($res){
			$ret = array('ret'=>1);
		}else{
			$ret = array('ret'=>0);
		}
		echo json_encode($ret);
	}

	//中奖信息页面
	public function awardedinfo(){
		$awardid = I('get.awardid');
		$applys = D(C('TABLE_APPLYS'));
		//查询所有中奖信息
		if(!empty($awardid)){
			$where['awardid'] = $awardid;
		}else{
			$where = null;
		}
		$count = $applys->where($where)->count();
		//每页显示条数
		$num = 10;
		$page = new \Think\Page($count,$num);
		$obj = pagestyle($page);
		$show = $obj->show();
		$result = $applys->order(array('id'=>'DESC'))->where($where)->limit($obj->firstRow.','.$obj->listRows)->select();

		$this->assign('count',$count);
		$this->assign('result',$result);
		$this->assign('page',$show);
		$this->assign('awardlist',$this->queryawardinfo(38));
		$this->assign('awardid',$awardid);
		$this->assign('timelist',$this->getawardtime());
		$this->assign('awardnum',$this->getawardnum());
		$this->display();
	}
	//获取第几次开奖
	protected function getawardnum(){
		$applys = D(C('TABLE_APPLYS'));
		$cishu = $applys->max('awarpnum');
		if(empty($cishu) || $cishu<1){
			return 0;
		}else{
			return $cishu;
		}
	}
	//获取开奖时间
	protected function getawardtime(){
		$applytime= D(C('TABLE_APPLYTIME'));
		$res = $applytime->select();
		return $res;
	}

	//删除中奖名单
	public function delete(){
		$id = I('post.id');
		$mobile = $this->queyrmobile($id);
		$applys = D(C('TABLE_APPLYS'));
		$res = $applys->where(array('id'=>$id))->delete();
		if($res){
			$ret = array('ret'=>1);
			//删除之后还原报名者的状态
			$apply = D(C('TABLE_APPLY'));
			$data['status'] = 0;
			$data['awarpnum'] = null;
			$apply->where(array('mobile'=>$mobile))->save($data);
		}else{
			$ret = array('ret'=>0);
		}
		echo json_encode($ret);
	}
	//根据中奖者的ID查询手机号码
	protected function queyrmobile($id){
		$applys = D(C('TABLE_APPLYS'));
		$res = $applys->field('mobile')->where(array('id'=>$id))->find();
		if($res){
			return $res['mobile'];
		}
	}

	//查询奖品信息
	protected function queryawardinfo($typeid){
		$award = D(C('TABLE_AWARD'));
		$res = $award->where(array('typeid'=>$typeid))->select();
		return $res;
	}
	//修改中奖信息
	public function awardedit(){
		$id = I('post.id');
		$awardid = I('post.awardid');
		$applys = D(C('TABLE_APPLYS'));
		$data['awardid'] = $awardid;
		$res = $applys->where(array('id'=>$id))->save($data);
		if($res || $res==0){
			$ret = array('ret'=>1);
		}else{
			$ret = array('ret'=>0);
		}
		echo json_encode($ret);
	}

	//未中奖操作页面
	public function awardecontrool(){
		$apply = D(C('TABLE_APPLY'));
		$where['status'] = 0;
		$count = $apply->where($where)->count();
		//每页显示条数
		$num = 10;
		$page = new \Think\Page($count,$num);
		$obj = pagestyle($page);
		$show = $obj->show();
		$result = $apply->order(array('id'=>'DESC'))->where($where)->limit($obj->firstRow.','.$obj->listRows)->select();

		$this->assign('count',$count);
		$this->assign('result',$result);
		$this->assign('page',$show);
		$this->assign('awardlist',$this->queryawardinfo(38));
		$this->display();
	}
	//搜索未中奖的信息
	public function noawardsearch(){
		$key = trim(I('post.key'));
		$apply = D(C('TABLE_APPLY'));
		$where = "(mobile like '%".$key."%' or name like '%".$key."%') and status=0";

		$count = $apply->where($where)->count();
		//每页显示条数
		$num = 10;
		$page = new \Think\Page($count,$num);
		$obj = pagestyle($page);
		$show = $obj->show();
		$result = $apply->order(array('id'=>'DESC'))->where($where)->limit($obj->firstRow.','.$obj->listRows)->select();
		$this->assign('count',$count);
		$this->assign('result',$result);
		$this->assign('page',$show);
		$this->assign('awardlist',$this->queryawardinfo(38));
		$this->assign('key',$key);
		$this->display('awardecontrool');
	}

	//设置未中奖成为中奖信息
	public function changeinfo(){
		$id = I('post.id');
		$awardid = I('post.awardid');
		//笨方法，通过奖品的ID得出中奖次数，哈哈~_~
		switch ($awardid){
			case 39:
				$awarpnum = 1;
				break;
			case 40:
				$awarpnum = 2;
				break;
			case 41:
				$awarpnum = 3;
				break;
			case 42:
				$awarpnum = 4;
				break;
		}
		if(!empty($id) && !empty($awardid)){
			$getNoawardinfo = $this->querynoaward($id);
			$apply = D(C('TABLE_APPLY'));
			$data['status'] = 1;
			$data['awarpnum'] = $awarpnum;
			$res = $apply->where(array('id'=>$id))->save($data);
			if($res){
				if($this->fromnoawardtoaward($getNoawardinfo,$awardid,$awarpnum)){
					$ret = array('ret'=>1);

				}else{
					$ret = array('ret'=>0);
				}
			}else{
				$ret = array('ret'=>0);
			}
			echo json_encode($ret);
		}

	}
	//根据ID查询未中奖信息
	protected function querynoaward($id){
		$apply = D(C('TABLE_APPLY'));
		$res = $apply->where(array('id'=>$id))->find();
		if($res){
			return $res;
		}else{
			return null;
		}
	}
	//从未中奖名单中插入到已中过奖的表中
	protected function fromnoawardtoaward($data,$awardid,$awarpnum){
		$applys = D(C('TABLE_APPLYS'));
		$str['mobile'] = $data['mobile'];
		$str['name'] = $data['name'];
		$str['fakecode'] = $data['fakecode'];
		$str['applydate'] = $data['applydate'];
		$str['status'] = 0;
		$str['awardid'] = $awardid;
		$str['awarpnum'] = $awarpnum;
		$str['awarpdate'] = date("Y-m-d H:i:s", time());
		$str['openid'] = $data['openid'];
		$res = $applys->add($str);
		return $res;
	}
	//修改开奖时间页面
	public function awartime(){
		$res = $this->queryawardinfo(38);
		$this->assign('awardlist',$res);
		$this->display();
	}
	//修改开奖时间
	public function changetime(){
		$id = I('post.id');
		$awardtime = trim(I('post.awardtime'));
		$applytime= D(C('TABLE_APPLYTIME'));
		$data['awardtime'] = $awardtime;
		$res = $applytime->where(array('awardid'=>$id))->save($data);
		if($res || $res==0){
			$ret = array('ret'=>1);
		}else{
			$ret = array('ret'=>0);
		}
		echo json_encode($ret);
	}
	//开奖
	public function akj(){
		$kaijang = A('Jgdraw');
		$result = $kaijang->kaijiang();
		//$a = array('ret',$result);;
		//echo json_encode($a);
	}

}